package com.woniuxy.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.dao.PermissionMapper;
import com.woniuxy.pojo.Permission;
import com.woniuxy.pojo.TreeNode;
import com.woniuxy.pojo.Users;
import com.woniuxy.service.system.PermissionService;
@Service
public class PermissionServiceImpl implements PermissionService{

	@Autowired
	private PermissionMapper permissionMapper;
	@Override
	public List<Permission> getPermissionByParentId(Integer pid) {
		return permissionMapper.selectByParentId(pid);
	}

	@Override
	public List<TreeNode> getTree(Integer pid) {
		List<Permission> pers=getPermissionByParentId(pid);
		List<TreeNode> tree = new ArrayList();
		for (Permission per : pers) {
			TreeNode treeNode=getTreeNode(per);
			tree.add(treeNode);
		}
		return tree;
	}

	@Override
	public TreeNode getTreeNode(Permission per) {
		TreeNode treeNode = new TreeNode();
		treeNode.setId(per.getPmId());
		treeNode.setChecked(false);
		treeNode.setPid(per.getPmPid());
		treeNode.setUrl(per.getPmGoto());
		treeNode.setName(per.getPmName());
		treeNode.setOpen(per.getPmOpen()==1?true:false);
		List<Permission> subPers = getPermissionByParentId(per.getPmId());
		if (subPers!=null) {
			List<TreeNode> child = new ArrayList();
			for (Permission subPer : subPers) {
				TreeNode subTreeNode = getTreeNode(subPer);
				child.add(subTreeNode);
			}
			treeNode.setChildren(child);
		}
		return treeNode;
	}

	@Override
	public List<Permission> getPermissionByRoleId(Integer rid) {
		// TODO Auto-generated method stub
		return permissionMapper.selectPersByRid(rid);
	}

	@Override
	public List<Permission> getPermissionByUser(Users user) {
		// TODO Auto-generated method stub
		return permissionMapper.getPermissionByUser(user.getUserId());
	}

}
